<template>
	<view>
		<uni-file-picker 
			limit="1" 
			return-type="object" 
			mode="grid" 
			:image-styles="imageStyles"
			@select="onSelect"
			@delete="onDelete"
			ref="idCardUpload"
		/>
	</view>
</template>

<script>
export default {
	data() {
		return {
		}
	},
	
	computed: {
		imageStyles() {
			return { 
				height: this.height, 
				width: this.width, 
				border:{
					color:"#eee",
					width:"1px",
					style:"solid"
				}
			}
		}
	},
	
	props: {
		openType: {
			type: String,
			default: 'account',
		},
		fileType: {
			type: String,
			default: '01',
			required: true
		},
		width: {
			type: Number,
			default: 158
		},
		height: {
			type: Number,
			default: 100
		},
		id_no: {
			type: String,
			default: "",
		},
		name: {
			type: String,
			default: "",
		}
	},
	methods: {
		onSelect( e ) {
			let file = e.tempFiles[0].file;
			
			if(file.fileType != "image") {
				this.$refs.idCardUpload.clearFiles(0);
				uni.showToast({
					icon: 'error',
					title: '请上传有效图片'
				})
				return;
			}
			
			if(file.size > 200 * 1024 * 1024) {
				this.$refs.idCardUpload.clearFiles(0);
				uni.showToast({
					icon: 'error',
					title: '文件最大200M'
				})
				return;
			}
			
			let formData = {
				type: this.fileType,
			}
			
			if(this.fileType == '04') {
				if(this.name == "") {
					this.$refs.idCardUpload.clearFiles(0);
					uni.showToast({
						icon: 'error',
						title: '请输入姓名'
					})
					return;
				}
				
				if(this.id_no == "") {
					this.$refs.idCardUpload.clearFiles(0);
					uni.showToast({
						icon: 'error',
						title: '请输入证件号'
					})
					return;
				}
				
				formData.name = this.name;
				formData.id_no = this.id_no;
			}
			
			uni.showLoading({
				title: '上传中...',
				mask: true
			});
			
			uni.uploadFile({
				url: getApp().globalData.host + '/util/uploadIdCard',
				filePath: file.path,
				name: 'id_card',
				formData,
				success: (res) => {
					let data = JSON.parse(res.data)
					if(data.success) {
						this.makeModelData(data.data)
						uni.showToast({
							icon: 'success',
							title: '识别成功'
						})
					} else {
						uni.showToast({
							icon: 'error',
							title: data.errMessage
						})
						this.$refs.idCardUpload.clearFiles(0);
					}
				},
				
				fail: () => {
					this.$refs.idCardUpload.clearFiles(0);
				},
				
				complete: () => {
					uni.hideLoading();
				}
			})
		},
		
		onDelete(e) {
			this.$refs.idCardUpload.clearFiles(0);
			
			let model = {}
			
			if(this.fileType == '01') {
				model = {
					id_card_front_no: '',
					id_card_front_path: ''
				}
			}
			
			if(this.fileType == '02') {
				model = {
					id_card_back_no: '',
					id_card_back_path: ''
				}
			}
			
			if(this.fileType == '04') {
				model = {
					face_no: '',
					face_path: ''
				}
			}
			
			if(this.fileType == '12') {
				model = {
					business_no: '',
					business_path: ''
				}
			}
			
			this.$emit('onSelect', model)
		},
		
		makeModelData(data) {
			
			if(this.fileType == '01') {
				if(data.sex == '男') {
					data.sex = 0
				}
				if(data.sex == '女') {
					data.sex = 1
				}
			}
			
			if(this.fileType == '02') {
				let date = ((data.id_date.replace('-',' ')).replaceAll('.','-')).split(' ')
				data.id_start_date = date[0]
				data.id_end_date = date[1] == '长期' ? '2099-12-31' : date[1]
			}
			
			if(this.openType == 'business') {
				let business_data ={}
				if(this.fileType == '01') {
					business_data = {
						company_id_name: data.realname,
						company_id_no: data.id_no,
						id_card_front_no: data.id_card_front_no,
						id_card_front_path: data.id_card_front_path,
					}
				}
				
				if(this.fileType == '02') {
					business_data = {
						company_opto: data.id_end_date,
						id_card_back_no: data.id_card_back_no,
						id_card_back_path: data.id_card_back_path,
					}
				}
				
				this.$emit('onSelect', business_data)
			} else {
				this.$emit('onSelect', data)
			}
			
		}
	}
}
</script>

<style>
</style>